package controllers

import (
	"encoding/json"
	"supershare/backend/models"

	"github.com/beego/beego/v2/server/web"
)

// MiniController 小程序接口控制器
type MiniController struct {
	web.Controller
}

// MiniVenueTypeController 小程序场馆类型控制器
type MiniVenueTypeController struct {
	web.Controller
}

// GetAll 获取所有场馆类型（无需分页）
func (c *MiniVenueTypeController) GetAll() {
	storeId, _ := c.GetInt64("store_id")

	var venueTypes []*models.VenueType
	var err error

	if storeId > 0 {
		store, err := models.GetStoreById(storeId)
		if err != nil {
			c.Data["json"] = map[string]interface{}{
				"success": false,
				"message": "获取门店信息失败: " + err.Error(),
			}
			c.ServeJSON()
			return
		}

		var typeIds []int64
		if store.VenueTypes != "" {
			err := json.Unmarshal([]byte(store.VenueTypes), &typeIds)
			if err != nil {
				c.Data["json"] = map[string]interface{}{
					"success": false,
					"message": "解析场地类型失败: " + err.Error(),
				}
				c.ServeJSON()
				return
			}
		}

		if len(typeIds) > 0 {
			venueTypes, err = models.GetVenueTypesByIds(typeIds)
		} else {
			// 如果没有关联的场地类型，返回空列表
			venueTypes = []*models.VenueType{}
		}

	} else {
		venueTypes, err = models.GetAllVenueTypes(0)
	}

	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取场馆类型列表失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 返回成功结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "获取场馆类型列表成功",
		"data": map[string]interface{}{
			"list": venueTypes,
		},
	}
	c.ServeJSON()
}
